﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TM.QWeibo.SDK.Objects;

namespace TM.QWeibo.Dal
{
    public class Friends : Base 
    {
        /// <summary>
        /// 实例
        /// </summary>
        public static Friends Instance {
            get {
                return new Friends();
            }
        }

        #region 私有变量

        /// <summary>
        /// API
        /// </summary>
        readonly SDK.API.Friends _Api = new SDK.API.Friends(OAuthInstance);

        private const string _downloadedIdolListStoreKey = Common.OauthHelper.DownloadedIdolListStoreKey;

        #endregion

        /// <summary>
        /// 获取制定用户的听众列表
        /// </summary>
        /// <param name="account"></param>
        /// <param name="startindex"></param>
        /// <param name="callback"></param>
        public void GetFanslist(string account, int startindex, Action<ICollection<SDK.Objects.UserInfo>> callback)
        {
            _Api.GetUserFanslist(account, Common.Params.PageListCount, startindex, (rs) => {
                var us = new List<SDK.Objects.UserInfo>();
                if (rs.Data != null && rs.Data.Info != null) {
                    us = rs.Data.Info;
                }
                callback(us);
            });
        }

        /// <summary>
        /// 获取制定用户的收听列表
        /// </summary>
        /// <param name="account"></param>
        /// <param name="startindex"></param>
        /// <param name="callback"></param>
        public void GetIdollist(string account, int startindex, Action<ICollection<SDK.Objects.UserInfo>> callback)
        {
            GetIdolList(account,startindex,(rs)=>
                                                {
                                                    var us = new List<SDK.Objects.UserInfo>();
                                                    if (rs.Data != null && rs.Data.Info != null)
                                                    {
                                                        us = rs.Data.Info;
                                                    }
                                                    callback(us);

                                                });
        }

        private void GetIdolList(string account, int startindex, Action<ResponseData<SDK.Objects.UserInfo>> callback)
        {
            _Api.GetUserIdollist(account, Common.Params.PageListCount, startindex, callback);
        }

        /// <summary>
        /// 获取下载的收听数据
        /// </summary>
        /// <returns></returns>
        public List<SDK.Objects.UserInfo> GetDownloadedIdolList()
        {
            if (_allUsersList==null || _allUsersList.Count < 1)
            {
                Common.Isolated.Get(_downloadedIdolListStoreKey, out _allUsersList);
            }
            return _allUsersList;
        }

        public void DeleteDownloadedIdolList()

        {
            Common.Isolated.Remove(_downloadedIdolListStoreKey);
        }

        public void DownloadedIdolListDispose()
        {
            _allUsersList =null;
        }

        private void GetMyIdolList(int startindex, Action<ResponseData<SDK.Objects.UserInfo>> callback)
        {
            _Api.GetIdollist(Common.Params.PageListCount, startindex, callback);
        }

        private void GetMySimpleIdolList(int startindex, Action<ResponseData<SDK.Objects.UserInfo>> callback)
        {
            _Api.GetSimpleIdollist(Common.Params.PageListCount, startindex, callback);
        }

        static List<SDK.Objects.UserInfo> _allUsersList = new List<SDK.Objects.UserInfo>();

        private Action _downloadIdolListCallback;

        /// <summary>
        /// 下载我所有收听的名单
        /// </summary>
        public void DownloadIdolList(Action before=null, Action callback=null)
        {
            _downloadIdolListCallback = callback;
            if(before!=null)
            {
                before();
            }
            _DownloadIdolList();
        }

        private void _DownloadIdolList()
        {
            var startindex =_allUsersList==null?0:_allUsersList.Count;
            if(startindex>0)
            {
                startindex = startindex - 1;
            }
            GetMySimpleIdolList(startindex, (rs) =>
                                         {
                                             try
                                             {
                                                     if (rs.Data != null && rs.Data.Info != null)
                                                     {
                                                         if(_allUsersList==null)
                                                         {
                                                             _allUsersList=new List<UserInfo>();
                                                         }
                                                         _allUsersList.AddRange(rs.Data.Info);
                                                     }
                                                     if (rs.Data != null && rs.Data.HasNext < 1)
                                                     {
                                                         _DownloadIdolList();
                                                     }

                                                     if (rs.Data == null || (rs.Data != null && rs.Data.HasNext > 0))
                                                     {
                                                         //END
                                                         Common.Isolated.Set(_downloadedIdolListStoreKey,_allUsersList);
                                                         if (_downloadIdolListCallback != null)
                                                         {
                                                             _downloadIdolListCallback();
                                                         }
                                                     }

                                             }
                                             catch
                                             {
                                             }

                                         });
        }

        /// <summary>
        /// 收听某人
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback"></param>
        public void Add(string name,Action<bool> callback) {

            _Api.Add(name, (rs) =>
            {
                if (rs != null) {
                    callback(rs.Errcode == SDK.Objects.EErrcode.Success);
                }
            });
        }

        /// <summary>
        /// 取消收听某人
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback"></param>
        public void Delete(string name, Action<bool> callback)
        {

            _Api.Delete(name, (rs) =>
            {
                if (rs != null)
                {
                    callback(rs.Errcode == SDK.Objects.EErrcode.Success);
                }
            });
        }

    }
}
